% Matlab Code for replication of "Optimal Debt Maturity and Firm Investment"
% by J. Jungherr and I. Schott, September 2020
% 
% This code generates Figure 2
cd(datafolder)

%% Load model output generated by Matlab
load Fig2

%% Create figure
figure(2);
spl             = 1; % subplot counter

subplot(3,2,spl); 
spl = spl+1;
plot(Fig2.debtgrid_plot/Fig2.Av_B,Fig2.ltd_pol_plot/Fig2.Av_BL,'LineWidth',2.0)
title('Long-term debt $\tilde{b}^L$','interpreter','latex')
set(gca,'Fontsize',16)
set(gca,'TickLabelInterpreter', 'latex');
xlim([0, Fig2.debtplot./Fig2.Av_B])
ylim([0, 1])

subplot(3,2,spl); 
spl = spl+1;
plot(Fig2.debtgrid_plot/Fig2.Av_B,100*Fig2.lev_pol_plot,'LineWidth',2.0)
title('Leverage (in $\%$)','interpreter','latex')
set(gca,'Fontsize',16)
set(gca,'TickLabelInterpreter', 'latex');
xlim([0, Fig2.debtplot./Fig2.Av_B])
ylim([0, 40])

subplot(3,2,spl); 
spl = spl+1;
plot(Fig2.debtgrid_plot/Fig2.Av_B,Fig2.cap_pol_plot/Fig2.Av_K,'LineWidth',2.0)
title({'Capital $k$'},'interpreter','latex')
set(gca,'Fontsize',16)
set(gca,'TickLabelInterpreter', 'latex');
xlim([0, Fig2.debtplot./Fig2.Av_B])
ylim([0.60, 0.85])

subplot(3,2,spl); 
spl = spl+1;
plot(Fig2.debtgrid_plot/Fig2.Av_B,Fig2.p_pol_plot,'LineWidth',2.0)
ylim([0.80, 0.96])
title('LTD bond price $p^L$','interpreter','latex')
set(gca,'Fontsize',16)
set(gca,'TickLabelInterpreter', 'latex');
xlim([0, Fig2.debtplot./Fig2.Av_B])
ylim([0.80, 0.96])

subplot(3,2,spl); 
spl = spl+1;
plot(Fig2.debtgrid_plot/Fig2.Av_B,100*Fig2.ltds_pol_plot,'LineWidth',2.0)
title({'LTD-Share (in $\%$)'},'interpreter','latex')
set(gca,'Fontsize',16)
set(gca,'TickLabelInterpreter', 'latex');
xlim([0, Fig2.debtplot./Fig2.Av_B])
ylim([0, 110])
xlabel('Existing debt $b$','interpreter','latex')

subplot(3,2,spl); 
spl = spl+1;
bar(Fig2.Bin_grid,Fig2.Bin,3)
title('Mass of firms $\mu$','interpreter','latex')
xlabel('Existing debt $b$','interpreter','latex')
set(gca,'Fontsize',16)
set(gca,'TickLabelInterpreter', 'latex');
legend({'$z=Z_1$','$z=Z_2$'},'FontSize',12,'location','northeast','interpreter','latex')
xlim([-0.02, Fig2.debtplot./Fig2.Av_B])
ylim([0, 0.20])

% Position the graph
set(gcf, 'Position',  [300, 200, 700, 400])

clear vars
cd(mainfolder)
